package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.dao.ReleverDao;
import com.cjack.frenchexcel.model.ReleverModel;
import com.cjack.frenchexcel.table.ReleverTable;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ReleverService {

    @Autowired
    private ReleverDao dao;

    public List<ReleverModel> list(ReleverModel model){
        Example<ReleverTable> example = Example.of( ModelUtils.copySignModel( model, ReleverTable.class));
        return ModelUtils.copyListModel( dao.findAll( example), ReleverModel.class);
    }

    public void save( ReleverModel model){
        ReleverTable table;
        if( model.getId() == null){//新增，把model里面的属性全部复制到新的table里面
            table = ModelUtils.copySignModel( model, ReleverTable.class);
        }else{//修改，把model里面的[非空]属性赋值到数据库里面查询的table里面
            table = dao.findOne( model.getId());
            ModelUtils.copySignModel( model, table);
        }
        dao.save( table);
    }
}
